package com.tianpengcc.service.impl;

import java.util.List;

import com.tianpengcc.dao.DirayDAO;
import com.tianpengcc.dao.DirayTypeDAO;
import com.tianpengcc.dao.impl.DirayDAOImpl;
import com.tianpengcc.dao.impl.DirayTypeDAOImpl;
import com.tianpengcc.model.DateType;
import com.tianpengcc.model.Diray;
import com.tianpengcc.model.PageBean;
import com.tianpengcc.service.DirayService;

public class DirayServiceImpl implements DirayService {
	private DirayDAO dirayDAO ;
	private DirayTypeDAO dirayTypeDAO ;
	
	public DirayServiceImpl(){
		this.dirayDAO = new DirayDAOImpl() ;
		this.dirayTypeDAO = new DirayTypeDAOImpl() ;
	}
	
	@Override
	public PageBean<Diray> getDirayPageBeanByLimit(int pageNumber, int pageSize)
			throws Exception {
		PageBean<Diray> pageBean = new PageBean<Diray>(pageNumber,pageSize) ;
		int total = dirayDAO.findTotalCount();
		List<Diray> list = dirayDAO.findByPage(pageBean);
		pageBean.setData(list);
		pageBean.setTotalRecord(total);
		
		return pageBean;
	}

	@Override
	public List<DateType> getDateTypeBySize(int dateTypeSize)
			throws Exception {
		
		List<DateType> list = dirayDAO.findDateTypeByPageBean(dateTypeSize);
		return list;
	}

	@Override
	public PageBean<Diray> getDirayPageBeanByDirayTypeIdAndLimit(
			int dirayTypeId, int pageNumber, int pageSize) throws Exception {
		PageBean<Diray> pageBean = new PageBean<Diray>(pageNumber,pageSize);
		List<Diray> list = dirayDAO.findByDirayTypeAndPage(dirayTypeId, pageBean);
		int totalRecord = dirayTypeDAO.findCountByDirayTypeId(dirayTypeId) ;
		pageBean.setTotalRecord(totalRecord);
		
		pageBean.setData(list);
		
		return pageBean;
	}

	
	@Override
	public PageBean<Diray> getByYearAndMonth(int year, int month,int pageNumber,int pageSize) throws Exception {
		PageBean<Diray> pageBean = new PageBean<Diray>(pageNumber,pageSize) ;
		int totalRecord = dirayDAO.findCountByYearAndMonth(year, month);
		List<Diray> list = dirayDAO.findByYearAndMonth(year, month, pageBean);
		pageBean.setData(list);
		pageBean.setTotalRecord(totalRecord);
		
		return pageBean;
	}

	@Override
	public PageBean<Diray> getByDirayNameLikeAndLimit(String dirayName,
			int pageNumber, int pageSize) throws Exception {
		PageBean<Diray> pageBean = new PageBean<Diray>(pageNumber,pageSize) ;
		List<Diray> list = dirayDAO.findByDirayNameLikeAndPage(dirayName,pageBean);
		int totalRecord = dirayDAO.findCountByDirayNameLike(dirayName) ;
		pageBean.setData(list);
		pageBean.setTotalRecord(totalRecord);
		
		return pageBean;
	}

	@Override
	public Diray getByDirayId(Integer dirayId) throws Exception {
		Diray diray = dirayDAO.findByDirayId(dirayId);
		return diray;
	}

	@Override
	public Diray doAdd(Diray diray) throws Exception {
		Diray diary = dirayDAO.doCreate(diray);
		return diary;
	}

	@Override
	public Diray doUpdate(Integer dirayId, Diray diray) throws Exception {
		Diray new_diray = dirayDAO.doUpdate(dirayId,diray);
		return new_diray;
	}

	@Override
	public void doDelete(Integer dirayId) throws Exception {
		dirayDAO.doDelete(dirayId);
	}

}
